<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [Conqweal] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.Conqweal.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/8/4 0004
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\event;

use app\admin\service\SystemLogService;
use support\Jrk\Tool;
use support\Request;

class SysLog
{
    public function handle($data)
    {
        $request = request();

        if ($request->isAjax() || $request->isPost()) {
            $method = strtolower($request->method());

            if (in_array($method, ['post', 'put', 'delete'])) {
               
                $url = $request->path();
                $ip     = $request->getRemoteIp();
                $params = $request->all();
                if (isset($params['s'])) {
                    unset($params['s']);
                }
                if (!empty($params)) {
                    //去除密码
                    foreach ($params as $k => $v) {
                        if (stripos($k, "password") !== false) {
                            unset($params[$k]);
                        }
                    }
                }
                $info = session("admin_info");

                if ($info) {
 
                    $data = [
                        'admin_id'    => isset($info['u_id']) ? $info['u_id'] : 0,
                        'url'         => $url,
                        'method'      => $method,
                        'title'       => isset($info['username']) ? $info['username'] : '',
                        'ip'          => $ip,
                        'os'          => Tool::getOS($request->header("user-agent")),
                        'brower'      => Tool::getBrowser($request->header("user-agent")),
                        'content'     => serialize($params),
                        'useragent'   => substr($request->header('user-agent'), 0, 255),
                        'create_time' => time(),
                    ];

                    SystemLogService::instance()->save($data);
                }
            }
        }
    }

}
